package com.kolon.business.mOI_.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI_.controller.MOIController")
public class MOIController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI_/mOI.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		// --- O.I 권한 생성 -----------------------------------------------------
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strQuery=Y&strGubun=JudgeAuth&userid=").append(userId);
		buffer.append("&companycd=").append(cpyCD);
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("Table");
		Iterator itXml = xmlList.iterator();
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			if ("T".equals(el.getChild("SUGJUDGE").getText()) || "D".equals(el.getChild("SUGJUDGE").getText())) {
				userMap.put("oiAuth", "Y");
				httpSession.setAttribute(Constant.SESSION_USER_INFO, userMap);
			}
		}
		buffer.delete(0, buffer.length());
		// ---------------------------------------------------------------------
		// URI 
		//buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=main&strMode=usersuggestcount&companycd=").append(cpyCD);
		buffer.append("&userid=").append(userId);
		// XML DOCUMENT 및 인증 처리
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("SUG_UserCount_T");
		itXml = xmlList.iterator();
		Map map = null;
		// XML 데이터  (총 건수, 당월제안, 아이디어, 실시제안)
		if (itXml.hasNext()) {
			map = new HashMap();
			Element el = (Element)itXml.next();
			map.put("iTCnt", el.getChild("idea_total_count").getText());
			map.put("nCnt1", el.getChild("ncount1").getText());
			map.put("nCnt2", el.getChild("ncount2").getText());
			map.put("nCnt3", el.getChild("ncount3").getText());
			map.put("theCnt", el.getChild("themecount").getText());
			map.put("totCnt", el.getChild("total_count").getText());
			map.put("cpyCD", el.getChild("companycd").getText());
			map.put("nMCnt", el.getChild("NowMonth_Count").getText());
		}
		buffer.delete(0, buffer.length());
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=main&strMode=usernonecount&companycd=").append(cpyCD);
		buffer.append("&userid=").append(userId);
		// XML DOCUMENT 및 인증 처리
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("SUG_UserNonCount_T");
		itXml = xmlList.iterator();
		// XML 데이터  (미실시, 미심사)
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("neCnt", el.getChild("non_exec_count").getText());
			map.put("wjCnt", el.getChild("wait_judge_count").getText());
			map.put("njCnt", el.getChild("non_judge_count").getText());
			map.put("sj", el.getChild("sug_judge").getText());
		}
		buffer.delete(0, buffer.length());
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=main&strMode=userprizecount&companycd=").append(cpyCD);
		buffer.append("&userid=").append(userId);
		// XML DOCUMENT 및 인증 처리
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("SUG_UserPrizeCount_T");
		itXml = xmlList.iterator();
		// XML 데이터  (포인트, 누계)
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("sP", el.getChild("sum_prize").getText());
			map.put("tP", el.getChild("total_prize").getText());
		}
		buffer.delete(0, buffer.length());
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=mylist&strMode=mysuggest&companycd=").append(cpyCD);
		buffer.append("&userid=").append(userId).append("&ViewRowCount=15&Page=1");
		// XML DOCUMENT 및 인증 처리
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("TableInfo");
		itXml = xmlList.iterator();
		// XML 데이터  (나의 제안 수)
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("myCnt", el.getChild("TotalRecordCnt").getText());
		}
		buffer.delete(0, buffer.length());
		
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		return modelAndView;
	}

}
